# coding:utf-8
from flask_wtf import FlaskForm
from flask_wtf.file import FileRequired, FileAllowed
from wtforms import FileField, StringField
from wtforms.validators import Optional

from ..forms import WphForm, NonValidatingSelectField, NotChoices
from app.axb.models import NumberPoolForLT
from app.op.models import Area
from app.op.constant import AreaType


class ImportNumberForm(FlaskForm):
    """
    虚拟号导入表单
    """
    files = FileField(
        '虚拟号文件',
        validators=[FileRequired(), FileAllowed(['xls', 'xlsx'])]
    )


class SearchNumberForm(WphForm):
    """
    created by chenhj at 2017/10/10

    """
    tel = StringField('虚拟号', validators=[Optional()])


class AreaNumberForm(FlaskForm):
    """
    add by zhouhl 2018-04-02
    按照地区启用/禁用虚拟号
    """
    area_id = NonValidatingSelectField('虚拟号地区',
        validators=[Optional()],
    )

    def __init__(self, *args, **kwargs):
        super(AreaNumberForm, self).__init__(*args, **kwargs)
        area_id_list = Area.query.filter(
            Area.type == AreaType.CITY.value,
            Area.id.in_(NumberPoolForLT.query.with_entities(NumberPoolForLT.area_id).distinct())
        ).all()
        self.area_id.choices = NotChoices + [
            (i.id, i.name) for i in area_id_list
            ]
